import { DB_PREFIX } from '@/common/constants/global.constant';
import { StatusEnum } from '@/common/enums';
import { MenuMeta } from '@/common/interfaces/menumeta.interface';
import { Column, CreateDateColumn, Entity, PrimaryGeneratedColumn, UpdateDateColumn } from 'typeorm';

/**
 * 开发环境中开启了 "synchronize": true，生产环境中禁止开启
 * 如果实体的类型或者长度等改变，就会导致库中该字段被直接清空
 * 所以在数据库建模时一旦建成切勿随意修改字段
 * 若非修改不可，必须使用迁移：migrate 的方式来更新数据库
 */

@Entity(`${DB_PREFIX}menus`)
export class Menu {
  @PrimaryGeneratedColumn()
  id: number;

  @Column({ type: 'varchar', length: 255, unique: true, nullable: false })
  name: string;

  @Column({ type: 'varchar', length: 255, nullable: true })
  path: string;

  @Column({ type: 'varchar', length: 255, nullable: true })
  component: string;

  @Column({ type: 'varchar', length: 255, nullable: true })
  redirect: string;

  @Column({ type: 'varchar', length: 255, nullable: true })
  parent: string;

  @Column({ type: 'int', default: 0 })
  sort: number;

  @Column('simple-array', { nullable: true })
  permissions: string[];

  @Column({ type: 'json', nullable: true })
  meta: MenuMeta;

  @Column({ type: 'varchar', length: 255, nullable: true })
  description: string;

  @Column({ type: 'varchar', length: 255, nullable: true })
  remark: string;

  @Column({ type: 'enum', enum: StatusEnum, default: StatusEnum.Enabled })
  status: StatusEnum;

  @CreateDateColumn({ type: 'timestamp' })
  createTime: Date;

  @UpdateDateColumn({ type: 'timestamp' })
  updateTime: Date;
}
